<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Capitalize;
use App\Models\ProjectCapitalize;
use Illuminate\Support\Facades\Log;

class CapitalizeService
{
    public function get($search){
        $sql = ProjectCapitalize::with('project')->with('capitalizes')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('name','like', '%'.$search.'%');
            });
        });

        return $sql;
    }

    public function insert($input,$details){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        $arr = [];
        foreach ($details as $key => $value) {
            $arr_ = [];
            $arr_['name'] = $value->name;
            $arr_['amount'] = $value->amount;
            $arr_['interest_rate'] = $value->interest_rate;
            $arr_['monthly_payment'] = $value->monthly_payment;
            $arr_['principal'] = $value->principal;
            $arr_['total_interest'] = $value->total_interest;
            $arr_['remarks'] = $value->remarks;
            $arr_['created_at'] = Date("Y-m-d H:i:s",time());
            $arr_['updated_at'] = Date("Y-m-d H:i:s",time());
            $arr[] = $arr_;
        }

        try {
            $data = ProjectCapitalize::create($res);
            foreach ($arr as $key => $value) {
                $arr[$key]['project_capitalize_id'] = $data->id;
            }
            Capitalize::insert($arr);
            if($data){
                return $data;
            }
            
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input,$details){
        $res = [];
        foreach ($input as $k => $v) {
            $v = $v === null ? '/' : $v;
            if($v){
                $res[$k] = $v;
            }
        }

        $arr = [];
        foreach ($details as $key => $value) {
            $arr_ = [];
            $arr_['project_capitalize_id'] = $res['id'];
            $arr_['name'] = $value->name;
            $arr_['amount'] = $value->amount;
            $arr_['interest_rate'] = $value->interest_rate;
            $arr_['monthly_payment'] = $value->monthly_payment;
            $arr_['principal'] = $value->principal;
            $arr_['total_interest'] = $value->total_interest;
            $arr_['remarks'] = $value->remarks;
            $arr_['created_at'] = Date("Y-m-d H:i:s",time());
            $arr_['updated_at'] = Date("Y-m-d H:i:s",time());
            $arr[] = $arr_;
        }
        unset($res['id']);
        try {
            $data = ProjectCapitalize::where('id',$input['id'])->update($res);
            Capitalize::where('project_capitalize_id',$input['id'])->delete();
            Capitalize::insert($arr);
            if($data){
                return $data;
            }

            
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

}